package com.wangjiyuan.shiwupai.ui_food.food.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.wangjiyuan.shiwupai.R;
import com.wangjiyuan.shiwupai.base.BaseApplication;
import com.wangjiyuan.shiwupai.bean.first_food.FoodBean;
import com.wangjiyuan.shiwupai.config.food.FoodConstant;
import com.wangjiyuan.shiwupai.ui_food.food_class.FoodClassActivity;
import com.wangjiyuan.shiwupai.utils.RecyclerViewAdapterHelper;

import java.util.List;

/**
 * Created by QXL on 2016/11/28.
 */

public class ItemFoodRecyclerAdapter extends RecyclerViewAdapterHelper<FoodBean.GroupBean.CategoriesBean> {
    private int margin;
    private String kind;

    public ItemFoodRecyclerAdapter(Context context, List<FoodBean.GroupBean.CategoriesBean> list,String kind) {
        super(context, list);
//        获取kind值
        this.kind=kind;
    }

    @Override
    public RecyclerView.ViewHolder onCreateMyViewHolder(ViewGroup parent, int viewType) {
        float density = mContext.getResources().getDisplayMetrics().density;
        margin = (int) (density * 20);
        View view = mInflater.inflate(R.layout.itemin_food,parent,false);
        return new ViewHolderFoodItemIn(view);
    }

    @Override
    public void onBindMyViewHolder(RecyclerView.ViewHolder holder, int position) {
        FoodBean.GroupBean.CategoriesBean categoriesBean = mList.get(position);
        String name = categoriesBean.getName();
        String imageUrl = categoriesBean.getImageUrl();
        Glide.with(mContext)
                .load(imageUrl)
                .fitCenter()
                .placeholder(R.drawable.white)
                .into(((ViewHolderFoodItemIn)holder).imageView);
        ((ViewHolderFoodItemIn) holder).textView.setText(name);
    }

    class ViewHolderFoodItemIn extends RecyclerView.ViewHolder implements View.OnClickListener{
        RelativeLayout relativeLayout_itemIn;
        ImageView imageView;
        TextView textView;
        public ViewHolderFoodItemIn(View itemView) {
            super(itemView);
            imageView= (ImageView) itemView.findViewById(R.id.imageView_itemin_food);
            RelativeLayout.LayoutParams layoutParams=new RelativeLayout.LayoutParams((BaseApplication.width-margin*2)/8,(BaseApplication.width-margin*2)/8);
//            设置水平居中
            layoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
            imageView.setLayoutParams(layoutParams);
            textView= (TextView) itemView.findViewById(R.id.textView_itemin_food);

            relativeLayout_itemIn= (RelativeLayout) itemView.findViewById(R.id.relativeLayout_itemIn);
            relativeLayout_itemIn.setOnClickListener(this);
        }

        @Override
        public void onClick(View view) {
            Intent intent=new Intent(mContext, FoodClassActivity.class);
            Bundle bundle=new Bundle();
            switch (view.getId()){
                case R.id.relativeLayout_itemIn:
                    int adapterPosition = getAdapterPosition();
                    FoodBean.GroupBean.CategoriesBean categories = mList.get(adapterPosition);
//                    传值 CategoriesBean对象
                    bundle.putSerializable(FoodConstant.FOOD_CategoriesBean_KEY, categories);
//                    传值 kind
                    bundle.putSerializable(FoodConstant.FOOD_KIND_KEY,kind);
                    intent.putExtras(bundle);
                    mContext.startActivity(intent);
                    break;
            }
        }
    }
}
